/*
 * SearchTableResults.java
 *
 * Created on Mar 3, 2011, 1:37:17 PM
 */
package org.workplicity.cia.ui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumn;
import org.workplicity.cia.table.SearchResultsTableModel;
import org.workplicity.cia.util.CIAHelper;
import org.workplicity.cia.worklet.ElogsWorklet;
import org.workplicity.elog.entry.ElogUser;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;
import org.workplicity.elog.entry.Elog;

/**
 *
 * @author Elog Team
 */
public class SearchTableResults extends javax.swing.JDialog {
    private ElogUser user = null;
    private ElogsWorklet worklet = ElogsWorklet.getInstance();
    private WorkletContext context = worklet.getContext();

    /** Creates new form SearchTableResults */
    public SearchTableResults() {
        initComponents();
        this.enableEdit();
        this.setLocationRelativeTo(null);
        this.setModal(false); // for multiple searches
        
        // set description column width
        TableColumn descCol =  resultsTable.getColumnModel().getColumn(5);
        descCol.setPreferredWidth(300);
        
         // set description column width
        TableColumn dateCol =  resultsTable.getColumnModel().getColumn(1);
        dateCol.setPreferredWidth(100);
        
        user = (ElogUser) Helper.whoAmI(context);
        this.setTitle("Netlog (" + user.getLogname() + ") >> Search >> Results");
    }
    
    public SearchTableResults(JFrame frame) {
        initComponents();
        this.enableEdit();
        this.setLocationRelativeTo(null);
        this.setModal(false); // for multiple searches
        
        // set description column width
        TableColumn descCol =  resultsTable.getColumnModel().getColumn(5);
        descCol.setPreferredWidth(300);
        
        // set description column width
        TableColumn dateCol =  resultsTable.getColumnModel().getColumn(1);
        dateCol.setPreferredWidth(100);
        
        user = (ElogUser) Helper.whoAmI(context);
        this.setTitle("Netlog (" + user.getLogname() + ") >> Search >> Results");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        resultsTable = new javax.swing.JTable();
        backButton = new javax.swing.JButton();
        doneButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);

        resultsTable.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        resultsTable.setModel(new org.workplicity.cia.table.SearchResultsTableModel());
        jScrollPane1.setViewportView(resultsTable);

        backButton.setFont(new java.awt.Font("Tahoma", 0, 12));
        backButton.setText("Back");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        doneButton.setFont(new java.awt.Font("Tahoma", 0, 12));
        doneButton.setText("Done");
        doneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doneButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(doneButton, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(doneButton)
                    .addComponent(backButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
        // Action for clicking the Done button
        dispose();
    }//GEN-LAST:event_doneButtonActionPerformed

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        // Action for clicking the Back button
        dispose();
    }//GEN-LAST:event_backButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e){

                }
                new SearchTableResults().setVisible(true);
            }
        });
    }

     //Sets the double click functionality to open the EditElogDialog box
    private void enableEdit()
    {
        System.out.println("ClickEnabled");

         resultsTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Click!");
                if (e.getClickCount() == 2) {
                    JTable target = (JTable) e.getSource();

                    final int row = target.getSelectedRow();

                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            editEntry(row);
                        }
                    });
                }
            }
        });
    }

    //Allows for editing of entries
    private void editEntry(int row) {
        Elog elog = null;

        SearchResultsTableModel searchTableModel = new SearchResultsTableModel();

        ElogUser owner = null;

        if(row != -1)
        {
            elog = searchTableModel.getRow(row);
            owner = CIAHelper.toUser(elog.getReposTitle());
        }
        else
        {
            elog = new Elog();
            owner = (ElogUser) Helper.whoAmI(WorkletContext.getInstance());
        }

        ElogEditDialog dialog = new ElogEditDialog(this, owner, elog);

        dialog.setVisible(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    private javax.swing.JButton doneButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable resultsTable;
    // End of variables declaration//GEN-END:variables

}
